import { Injectable } from '@angular/core';
import { Router, Resolve, ActivatedRouteSnapshot } from '@angular/router';

import { Home } from './home';
import { HomeService } from './home.service';

@Injectable()
export class HomeDetailResolve {
  constructor(
    private hs: HomeService,
    private router: Router) { }

  public resolve(route: ActivatedRouteSnapshot): Promise<Home> | boolean {
    const id = +route.params['id'];

    return this.hs.getHome(id).then((home) => {
      if (home) {
        return home;
      } else { // id not found
        this.router.navigate(['/home']);
        return false;
      }
    });
  }

}
